{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<tensorflow.python.keras.layers.convolutional.Conv2D at 0x1daf8a4bba8>,\n",
       " <tensorflow.python.keras.layers.pooling.MaxPooling2D at 0x1daf8a6a0b8>,\n",
       " <tensorflow.python.keras.layers.convolutional.Conv2D at 0x1daf8a6a320>,\n",
       " <tensorflow.python.keras.layers.pooling.MaxPooling2D at 0x1daf8a6a7b8>,\n",
       " <tensorflow.python.keras.layers.core.Flatten at 0x1daf8a6aa20>,\n",
       " <tensorflow.python.keras.layers.core.Dense at 0x1daf8a6aba8>,\n",
       " <tensorflow.python.keras.layers.core.Dropout at 0x1daf8a6aef0>,\n",
       " <tensorflow.python.keras.layers.core.Dense at 0x1daf88e0b38>]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import cv2\n",
    "import tensorflow as tf\n",
    "from tensorflow.keras import Model,layers\n",
    "\n",
    "\n",
    "class CNN_WORK(Model):\n",
    "    def __init__(self):\n",
    "        super(CNN_WORK,self).__init__()\n",
    "        self.conv1 = layers.Conv2D(32,kernel_size=5,activation=tf.nn.relu)\n",
    "        self.maxpool1 = layers.MaxPool2D(2,strides=2)\n",
    "        \n",
    "        self.conv2 = layers.Conv2D(64,kernel_size=3,activation=tf.nn.relu)\n",
    "        self.maxpool2 = layers.MaxPool2D(2,strides=2)\n",
    "        \n",
    "        self.flatten = layers.Flatten()\n",
    "        self.fc1 = layers.Dense(1024)\n",
    "        self.dropout = layers.Dropout(rate=0.5)\n",
    "        self.out = layers.Dense(2)\n",
    "    \n",
    "    def call(self,x,is_training=False):\n",
    "       # x = tf.reshape(x,[-1,64,64,3])\n",
    "        \n",
    "        x = self.conv1(x)\n",
    "        x = self.maxpool1(x)\n",
    "        x = self.conv2(x)\n",
    "        x = self.maxpool2(x)\n",
    "        \n",
    "        x = self.flatten(x)\n",
    "        x = self.fc1(x)\n",
    "        x = self.dropout(x,training=is_training)\n",
    "        x = self.out(x)\n",
    "        \n",
    "        \n",
    "        if not is_training:\n",
    "            x = tf.nn.softmax(x)\n",
    "        return x\n",
    "model = CNN_WORK()\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "cap = cv2.VideoCapture(0)\n",
    "\n",
    "face_cascade = cv2.CascadeClassifier('C:\\\\Users\\Wuhuipeng\\AppData\\Local\\Programs\\Python\\Python36\\Lib\\site-packages\\cv2\\data/haarcascade_frontalface_alt.xml')\n",
    "\n",
    "while True:\n",
    "    ret,frame = cap.read()\n",
    "\n",
    "    gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)\n",
    "\n",
    "    faces = face_cascade.detectMultiScale(gray,scaleFactor=1.2,minNeighbors=5,minSize=(5,5))\n",
    "\n",
    "    for (x,y,z,t) in faces:\n",
    "        img = frame[y:y+t,x:x+z]\n",
    "        try:\n",
    "            img = cv2.resize(img,(64,64))\n",
    "            img = tf.cast(img,tf.float32) / 255.0\n",
    "            img = tf.reshape(img,[-1,64,64,3])\n",
    "        \n",
    "            pred = model(img)\n",
    "            pred = tf.argmax(pred,axis=1).numpy()\n",
    "        except:\n",
    "            pass\n",
    "        if(pred[0]==1):\n",
    "            cv2.putText(frame,\"wuhuipeng\",(x-10,y-10),cv2.FONT_HERSHEY_SIMPLEX,1.2,(255,255,0),2)\n",
    "        \n",
    "        cv2.rectangle(frame,(x,y),(x+z,y+t),(0,255,0),2)\n",
    "    cv2.imshow('find faces',frame)\n",
    "    if cv2.waitKey(1)&0xff ==ord('q'):\n",
    "        break\n",
    "cap.release()\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
